package com.dk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.config.WebSocketServer;
import com.dk.entity.DtChat;
import com.dk.mapper.DtChatMapper;
import com.dk.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {
    @Autowired
    TestMapper testMapper;

    @Autowired
    DtChatMapper dtChatMapper;

    @GetMapping("index")
    public ResponseEntity<String> index(){
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping("page")
    public ModelAndView page(){
        return new ModelAndView("websocket");
    }

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        WebSocketServer.sendInfo(message,toUserId);
        //webSocketServer.onMessage(message);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }
    @RequestMapping("test")
    public List<DtChat> test(Long uid,Long bid,Long sid){
        QueryWrapper queryWrapper = new QueryWrapper();
        Map<String,Object> mapOrder = new HashMap<>();
        mapOrder.put("uid",1);
        mapOrder.put("bid",1);
        mapOrder.put("sid",1);
        queryWrapper.allEq(mapOrder,true);
        List<DtChat> dtChats = dtChatMapper.selectList(queryWrapper);
        return dtChats;
    }
}